package com.wootion.dizhi.comon;

import com.alibaba.fastjson.JSONObject;
import com.wootion.dizhi.runData.GlobalData;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.Date;
import java.util.Optional;

@Component
public class TimeHandler {

    private DateTimeFormatter timeFormatter;

    private SimpleDateFormat simpleDateFormat;

    public TimeHandler(String format,String formater) {
        this.simpleDateFormat = new SimpleDateFormat(format);
        this.timeFormatter = DateTimeFormatter.ofPattern(formater);;
    }

    public TimeHandler() {
        this("yyyy-MM-dd HH:mm:ss","yyyy-MM-dd HH:mm:ss");
    }

    public TimeHandler(String format) {
        this(format,"yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取当前时间的字符串格式
     * @return String
     */
    public String getTimeAsString(){
        return simpleDateFormat.format(new Date());
    }

    /**
     * 获取指定时间的字符串格式
     * @param time
     * @return String
     */
    public String getTimeAsString(Object time){
        if (time instanceof Date){
            return getTimeAsStringFromDate((Date) time);
        }
        return null;
    }

    /**
     * 获取Date类型时间的字符串格式
     * @param time
     * @return String
     */
    private String getTimeAsStringFromDate(Date time){
        return simpleDateFormat.format(new Date());
    }

    public Boolean isTimeAfterTime(Date time){
        Date date = new Date();
        return time.after(date);
    }

    /**
     * 时间对比
     * @param time
     * @param type
     * @param size
     * @return
     */
    public Boolean isTimeAfterTime(Date time, String type, Integer size) {
        LocalDateTime localDateTime = LocalDateTime.now();
        if (!Optional.ofNullable(time).isPresent()){
            return true;
        }
        switch (type) {
            case "mm": {
                LocalDateTime localDateTime1 = localDateTime.minusMinutes(size);
                ZoneId zoneId = ZoneId.systemDefault();
                ZonedDateTime zdt = localDateTime1.atZone(zoneId);
                Date date = Date.from(zdt.toInstant());
                return time.after(date);
            }
            default: {
                throw new RuntimeException(type + "没有可匹配的时间单位");
            }
        }
    }

    public <T>Comparator<T> getComparator(String field){
        Comparator<T> comparing = Comparator.comparing(jsonObject1 -> LocalDateTime.parse(((JSONObject) jsonObject1).getString(field), timeFormatter));
        return comparing;
    }

    /**
     * 获取自定义对象比较器
     * @param tClass
     * @param fileName
     * @return
     * @param <T>
     */
    public <T>Comparator<T> getComparator(Class<T> tClass,String fileName){
        Field field;
        try {
            field = tClass.getDeclaredField(fileName);
            field.setAccessible(true);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("TimeHandler:"+fileName+"字段不存在");
        }
       return Comparator.comparing(o -> {
            try {
                Object fieldValue = field.get(o);
                if (fieldValue instanceof LocalDateTime){
                    return (LocalDateTime)fieldValue;
                }else if (fieldValue instanceof String) {
                    return LocalDateTime.parse((String) fieldValue, timeFormatter);
                }else if (fieldValue instanceof Date) {
                    return ((Date)fieldValue).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                }else {
                   throw new IllegalArgumentException("不支持的字段类型");
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException("字段访问错误");
            }
        });
    }
}
